package com.cvit.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 * */
public class FileUpload {
	
	/**
	 * 删除服务器文件
	 * */
	public static boolean deleteServerFile(String imgpath){
		boolean delete_flag = false;
		String b=imgpath.replace("\\","/");
		File file = new File(b);
		if (file.exists() && file.isFile() && file.delete())
	        delete_flag = true;
	       else
	        delete_flag = false;
	    return delete_flag;
	}
	
	/**
	 * 外部接口图片下载到本地
	 * */
	public static String download(String urlString, String filename,HttpServletRequest request) throws Exception {  
        URL url = new URL(urlString);  // 构造URL  
        URLConnection con = url.openConnection(); // 打开连接 
        String img_path = "admissions_images/";
		String dir = request.getSession().getServletContext().getRealPath("/");//总路径
		StringBuilder path = new StringBuilder(dir + img_path);
        con.setConnectTimeout(5*1000);  //设置请求超时为5s 
        InputStream is = con.getInputStream();   // 输入流 
        byte[] bs = new byte[1024];  // 1K的数据缓冲  
        int len;  // 读取到的数据长度  
	    File sf=new File(path.toString());// 输出的文件流 
	       if(!sf.exists()){  
	           sf.mkdirs();  
	       }  
	    OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename+".jpg");  
	       while ((len = is.read(bs)) != -1) {  // 开始读取  
	          os.write(bs, 0, len);  
	       }  
	        os.close();  // 完毕，关闭所有链接  
	        is.close();
	        String image_path = img_path + filename + ".jpg";
	        return image_path;
    	}   
	
	/**
     * 下载图片压缩包
     * @param response
     * @throws FileNotFoundException
     * @throws IOException
     */
	public static void downloadFileByOutputStream(HttpServletResponse response,HttpServletRequest request,String zipFilePath,String fileNames)
            throws FileNotFoundException, IOException {
        String realPath = zipFilePath;//获取要下载的文件的绝对路径
        String fileName = fileNames;//获取要下载的文件名
        //设置content-disposition响应头控制浏览器以下载的形式打开文件，中文文件名要使用URLEncoder.encode方法进行编码，否则会出现文件名乱码
        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8")+".zip");
        InputStream in = new FileInputStream(realPath+fileName+".zip");//获取文件输入流
        int len = 0;
        byte[] buffer = new byte[1024];
        OutputStream out = response.getOutputStream();
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer,0,len);//将缓冲区的数据输出到客户端浏览器
        }
        in.close();
        out.close();
    }
	
	/**
     * 下载准考证
     * @param response
     * @throws FileNotFoundException
     * @throws IOException
     */
	public static void downloadTicketByOutputStream(HttpServletResponse response,HttpServletRequest request,String docFilePath,String fileNames)
            throws FileNotFoundException, IOException {
        String realPath = docFilePath;//获取要下载的文件的绝对路径
        String fileName = fileNames;//获取要下载的文件名
        //设置content-disposition响应头控制浏览器以下载的形式打开文件，中文文件名要使用URLEncoder.encode方法进行编码，否则会出现文件名乱码
        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8")+".doc");
        InputStream in = new FileInputStream(realPath+fileName+".doc");//获取文件输入流
        int len = 0;
        byte[] buffer = new byte[1024];
        OutputStream out = response.getOutputStream();
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer,0,len);//将缓冲区的数据输出到客户端浏览器
        }
        in.close();
        out.close();
    }
	
	
	
	 // 判断文件是否存在
	     public static boolean judeFileExists(String filsePath) {
	    	 File file = new File(filsePath);
	         if (file.exists()==true) {
	             return true;
	         } else {
	             return false;
	         }
	 
	     }
	
}